
<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px; width: 100%;"
        >
          <el-col :span="20">
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <el-col>
                <!-- 下拉菜单 element -->
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <!-- 下拉菜单 -->
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="addPart('')">添加子部门</el-dropdown-item>

                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 用一个行列布局 -->
        <el-tree
          :data="list"
          :default-expand-all="true"
        >
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--
      #default === slot-scope
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
    -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addPart(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="editPart(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="delPart(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        :title="title"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        @close="hClose"
      >
        <dept-dialog v-if="showDialog" :id="curId" :is-edit="isEdit" @success="hSuccess" @close="hClose" />
      </el-dialog>

    </div>
  </div>

</template>
<script>
import { tranListToTreeData } from '@/utils'
import { delDepartment, getDepartments } from '@/api/department'
import deptDialog from './deptDialog.vue'
export default {
  name: 'DepartmentPage',
  components: { deptDialog },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      showDialog: false,

      curId: '',
      isEdit: false
    }
  },
  computed: {
    title() {
      if (this.isEdit) {
        return '编辑部门'
      } else {
        return '添加部门'
      }
    }
  },
  created() {
    this.loadDepartmentInfo()
  },
  methods: {
    async loadDepartmentInfo() {
      try {
        const res = await getDepartments()
        // console.log(res)
        res.data.depts.shift()
        // console.log(company)
        this.list = tranListToTreeData(res.data.depts)
        // console.log(res)
      } catch (err) {
        console.log(err)
      }
    },
    addPart(id) {
      try {
        this.curId = id
        this.isEdit = false
        this.showDialog = true
      } catch (error) { console.log(error) }
    },
    editPart(id) {
      try {
        this.isEdit = true
        this.showDialog = true
        this.curId = id
      } catch (error) { console.log(error) }
    },
    hSuccess() {
      this.loadDepartmentInfo()
      if (this.isEdit) {
        this.$message.success('修改成功')
      } else {
        this.$message.success('添加成功')
      }
      this.hClose()
    },
    hClose() {
      this.id = ''
      this.isEdit = false
      this.showDialog = false
    },
    delPart(id) {
      this.$confirm('是否确认删除改部门?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        try {
          const res = await delDepartment(id)
          // console.log(res)
          this.loadDepartmentInfo()
          this.$message.success(res.message)
        } catch (error) {
          console.log(error)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }

  }
}
</script>

<style lang="scss" scoped>
.box-card{
  margin: 0 auto;
  width: 760px;
  color: #fff;
  ::v-deep .el-card__header{
    background-color: #66b1ff;

  }
  span{
    color:#fff;
    font-size: 20px;

  }
  .el-button{
     color:#fff;
    font-size: 20px;
  }
}
</style>
